package chapter4;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

public class Test {
    @org.junit.Test
    public void test(){
        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH) );

        IntStream evenNumbers = IntStream.rangeClosed(1, 100)
                .filter(n -> n % 2 == 0);
        System.out.println(evenNumbers.count());


        OptionalInt maxCalories = menu.stream().mapToInt(Dish::getCalories)
                .max();
        int max = maxCalories.orElse(1);
        System.out.println(max);


        int calories = menu.stream()
                       .mapToInt(Dish::getCalories)
                       .sum();
        System.out.println(calories);


        IntStream intStream = menu.stream().mapToInt(Dish::getCalories);
        Stream<Integer> stream = intStream.boxed();

        List<String> threeHighCaloricDishNames =
                menu.stream()
                         .filter(d->d.getCalories()>300)
                         .map(Dish::getName)
                         .limit(3)
                         .collect(toList());
    List<Dish> meat =
                menu.stream()
                    .filter(d->Dish.Type.MEAT.equals(d.getType()))
                    .collect(toList());


        List<String> words = Arrays.asList("Hello","World");
        List<String> uniqueCharacters =
                words.stream()
                .map(w->w.split(""))
                .flatMap(Arrays::stream)
                .distinct()
                .collect(toList());


        List<Integer> number = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> numberDouble = number.stream()
                .map(n->n=n*n)
                .collect(toList());


        List<Integer> numbers1 = Arrays.asList(1, 2, 3);
        List<Integer> numbers2 = Arrays.asList(3, 4);
        List<int[]> pairs =
                numbers1.stream()
                        .flatMap(i -> numbers2.stream()
                                .map(j -> new int[]{i, j})
                        )
                        .collect(toList());
        if(menu.stream().anyMatch(Dish::isVegetarian)){
        //    System.out.println("The menu is (somewhat) vegetarian friendly!!");
        }

        menu.stream()
                .filter(Dish::isVegetarian)
                .findAny()
                .ifPresent(d -> System.out.println(d.getName()));

        int sum1= number.stream().reduce(0,Integer::sum);
        Optional<Integer> sum = number.stream().reduce(Integer::max);
        System.out.println(sum);

      }


}
